package components.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.utils.ByteArray;
	import flash.utils.getQualifiedClassName;

	public class ObjectUtils
	{
		/**
		 * 变量是否是简单类型 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function isSimpleValue( value:Object ):Boolean
		{
			if( value is int ) return true;
			if( value is Number ) return true;
			if( value is Boolean ) return true;
			if( value is String ) return true;
			if( value is uint ) return true;
			return false;
		}
		
		public static function getObjectPropertys( object:Object ):Array
		{
			if( object==null ) return null;
			
			var arrChild:Array = [];
			var opt:ObjectPropertyType;
			if( object is DisplayObjectContainer )
			{
				var dis:DisplayObjectContainer = object as DisplayObjectContainer;
				for( var j:int=0; j<dis.numChildren; j++ )
				{
					var d:DisplayObject = dis.getChildAt(j);
					opt = new ObjectPropertyType();
					opt.canModify = false;
					opt.className = getQualifiedClassName(d);
					opt.name = d.name;
					arrChild.push( opt );
				}
			}
			
			var byte:ByteArray = new ByteArray();
			byte.writeObject( object );
			byte.position = 0;
			
			var obj:Object = byte.readObject();
			var arrProperty:Array = [];
			for( var i:String in obj )
			{
				opt = new ObjectPropertyType();
				opt.name = i;
				var b:Boolean = false;
				var o:Object = object[i];
				if( isSimpleValue( o ) )
				{
					b = true;
					try
					{
						object[i] = o;
					}
					catch( e:Error )
					{
						b = false;
					}
				}
				opt.canModify = b;
				arrProperty.push( opt );
			}
			arrProperty.sortOn( "property" );
			
			return [arrChild, arrProperty];
		}
	}
}